package com.jair.common.config;

import com.jair.common.Inteceptor.SqlLogInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * SQL日志配置类
 * 用于注册SQL监听拦截器
 *
 * @author jair
 * @since 2025-01-01
 */
@Configuration
public class SqlLogConfig {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;


    /**
     * 在所有SqlSessionFactory初始化完成后，添加SQL日志拦截器
     */
    @PostConstruct
    public void addInterceptor() {
        // 只对第一个SqlSessionFactory添加拦截器，避免重复注册
        if (!sqlSessionFactoryList.isEmpty()) {
            // 手动创建拦截器实例，避免重复
            SqlLogInterceptor sqlLogInterceptor = new SqlLogInterceptor();
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryList.get(0);
            sqlSessionFactory.getConfiguration().addInterceptor(sqlLogInterceptor);
        }
    }
}